package com.wectrls.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName CustomAuthenticationFailureHandler
 * @Description //TODO 用户验证失败处理器
 * @Author 陈建
 * @Date 2019/2/19 11:19
 * @Version 2.0
 */
@Component
public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler {
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException {
        httpServletResponse.setContentType("application/json;charset=UTF-8");
       /* CommonVO commonVO = new CommonVO(RESPONSE_ENUM.FAIL_409.getCode(), e.getMessage());
        httpServletResponse.getWriter().write(objectMapper.writeValueAsString(commonVO));*/
        httpServletResponse.setStatus(401);
    }
}
